<?php

namespace Sisesporte\Utils;

use Zend\Mail,
    Zend\Mail\Transport\Smtp as SmtpTransport,
    Zend\Mail\Transport\SmtpOptions;
use Zend\Mime\Message as MimeMessage,
    Zend\Mime\Part;

class Email
{

    /**
     * @FIXME
     * @param array $to
     * @param type $subject
     * @param type $html
     */
    public function sendEmail (array $to, $subject, $html)
    {
        try {
            $mail = new Mail\Message();
            $mail->setEncoding("UTF-8");
            $mail->setFrom('sisesporte.confidere@gmail.com', 'SisEsporte');
            $mail->setSubject($subject);
            $mail->setBody($this->_mimeType($html));

            foreach ($to as $key => $value) {
                $mail->addTo($key, $value);
            }

            $transport = new SmtpTransport();
            $transport->setOptions($this->_configSmtp());
            $transport->send($mail);
            return true;
        } catch (Exception $e) {
            return false;
        }
    }

    /**
     * Trata o MimeType
     * @param type $html
     * @return type MimeMessage
     */
    private function _mimeType ($html)
    {
        $bodyPart = new MimeMessage();

        $htmlBody = new Part($html);
        $htmlBody->type = "text/html";
        $bodyPart->setParts(array($htmlBody));

        return $bodyPart;
    }

    /**
     * Configurações do Smtp
     * @return \Zend\Mail\Transport\SmtpOptions
     */
    private function _configSmtp ()
    {
        return new SmtpOptions(array(
            "name" => "gmail",
            "host" => "smtp.gmail.com",
            "port" => 587,
            "connection_class" => "login",
            "connection_config" => array(
                "username" => "sisesporte.confidere@gmail.com",
                "password" => "confidere@123",
                "ssl" => "tls"
            )
        ));
    }

}